package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import entity.tSlideImg;



import service.tSlideImgService;

public class SlideImgAction {
	private int id;
	private String title;
	private String imgURl;
	private String localFileURl;
	private tSlideImgService tSlideImgService;

	public String addSlideImg() {
		this.tSlideImgService.add(title, imgURl,localFileURl);
		return "sa2";
	}
	
	public String delSlideImg() {
		this.tSlideImgService.delete(id);
		return "success";
	}
	public String getSlideImg(){
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
	    List<tSlideImg> tSlideImg=new ArrayList<tSlideImg>();
	    tSlideImg=this.tSlideImgService.getAll();
	    request.setAttribute("lists",tSlideImg);
		return "success";
	}
	public void getAjaxSlideImg() throws IOException{
		
		JSONArray jsonArray = new JSONArray();
		List<tSlideImg> tSlideImg=new ArrayList<tSlideImg>();
	    tSlideImg=this.tSlideImgService.getAll();
		for (tSlideImg slideImg : tSlideImg) {
			JSONObject t = new JSONObject();
			t.put("imgURl", slideImg.getImgURl());
			t.put("title", slideImg.getTitle());
			jsonArray.add(t);
		}
		JSONObject result = new JSONObject();
		result.put("data", jsonArray);
		HttpServletResponse response = ServletActionContext.getResponse();
		// 统一编码，避免页面显示出现乱码
		response.setCharacterEncoding("utf-8");
		// 将信息传到页面
		PrintWriter out = response.getWriter();
		out.write(result.toString());
		out.flush();
		out.close();
	}
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getImgURl() {
		return imgURl;
	}

	public void setImgURl(String imgURl) {
		this.imgURl = imgURl;
	}

	public tSlideImgService gettSlideImgService() {
		return tSlideImgService;
	}

	public void settSlideImgService(tSlideImgService tSlideImgService) {
		this.tSlideImgService = tSlideImgService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLocalFileURl() {
		return localFileURl;
	}

	public void setLocalFileURl(String localFileURl) {
		this.localFileURl = localFileURl;
	}
	
}
